package com.sx.baseproject.framework.tools.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.FutureTarget;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.annotations.NonNull;

/**
 * Time       : 2020/07/07 上午11:03
 * Author     : sion
 * Description:
 */
public class ImageUntil {

    public Bitmap drawToBitmap(Drawable draw) {
        return null;
    }

    public Drawable bitmapToDraw(Bitmap bitmap) {
        return null;
    }

    public Bitmap getBitmap(File file) {
        return BitmapFactory.decodeFile(file.getPath());
    }



    public Observable<Bitmap> glideToBitmap(Context mContext, String url) {
        return Observable.create(new ObservableOnSubscribe<Bitmap>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Bitmap> emitter) throws Exception {
                FutureTarget<Bitmap> submit = Glide.with(mContext)
                        .asBitmap()
                        .load(url)
                        .submit();
                if (submit == null || submit.get() == null) {
                    emitter.onError(new Throwable("图片加载失败"));
                    emitter.onComplete();
                } else {
                    emitter.onNext(submit.get());
                    emitter.onComplete();
                }
            }
        });

    }

    public Observable<Bitmap> glideToBitmapCircleCrop(Context mContext, String url, int size) {
        return Observable.create(new ObservableOnSubscribe<Bitmap>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Bitmap> emitter) throws Exception {
                FutureTarget<Bitmap> submit = Glide.with(mContext)
                        .asBitmap()
                        .load(url)
                        .override(size)
                        .circleCrop()
                        .submit();
                if (submit == null || submit.get() == null) {
                    emitter.onError(new Throwable("图片加载失败"));
                    emitter.onComplete();
                } else {
                    emitter.onNext(submit.get());
                    emitter.onComplete();
                }
            }
        });

    }


}
